#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
from core import main
from conf import setting
import os
import configparser
class Host_manage(object):
    def __init__(self):
        pass
    def menu(self):
        choise_menu='''
        \033[1;31m
        ---------manage----------
        1. 创建主机组
        2. 创建主机
        3. 查看主机
        4. 修改主机
        5. 删除主机
        6. 返回上一级菜单
        7. 退出
        \033[0m

        '''
        menu_dict={
            '1':Host_manage.create_host_group,
            '2':Host_manage.create_host,
            '3':Host_manage.select_host,
            '4':Host_manage.update_host,
            '5':Host_manage.delete_host,
            '6':Host_manage.go_back,
            '7':'exit'
        }
        while True:
            print(choise_menu)
            choise = input('请选择>>>')
            if choise in menu_dict:
                if choise == '7':
                    exit()
                else:
                    menu_dict[choise](self)
            else:
                print('输入错误！')

    def create_host_group(self):
        host_path = setting.HOST_FILE_PATH
        new_group = input('请输入新的主机组名>>>')
        new_group_file = os.path.join(host_path,new_group) #获得新主机组存放地址
        if os.path.isfile(new_group_file) is not True:
            f = open(new_group_file,'w')
            f.close()
            print('创建主机组成功！')
        else:
            print('主机组已存在！')

    def create_host(self):
        host_path = setting.HOST_FILE_PATH
        group_list = os.listdir(host_path)
        for index, item in enumerate(group_list): #列出主机组
            print(index, item)
        group_choise = input('请选择主机组>>>')
        if group_choise.isdigit():
            if group_list[int(group_choise)]:
                save_path = os.path.join(setting.HOST_FILE_PATH,group_list[int(group_choise)]) #打开所选取的主机组文件
                #print(save_path)
                config = configparser.ConfigParser()
                config.read(save_path)
                hostname = input('hostname:')
                port = input('port:')
                user = input('user:')
                passwd = input('passwd:')
                config.add_section(hostname) #添加新主机
                config.set(hostname, 'hostname', hostname)
                config.set(hostname, 'port', port)
                config.set(hostname, 'user', user)
                config.set(hostname, 'passwd', passwd)
                config.write(open(save_path,'w'))
                print('保存成功！')
            else:
                print('输入错误！')
        else:
            print('输入错误！')

    def update_host(self):
        host_path = setting.HOST_FILE_PATH
        group_list = os.listdir(host_path)
        for index, item in enumerate(group_list):
            print(index, item)
        group_choise = input('请选择主机组>>>')
        if group_choise.isdigit():
            if group_list[int(group_choise)]:
                save_path = os.path.join(setting.HOST_FILE_PATH, group_list[int(group_choise)])
                config = configparser.ConfigParser()
                config.read(save_path)
                host_list = config.sections()
                for index, item in enumerate(host_list):
                    print(index, item)
                host_choise = input('请选择需要修改的主机>>>')
                if host_choise.isdigit():
                    if host_list[int(host_choise)]:
                        hostname = input('hostname:')
                        port = input('port:')
                        user = input('user:')
                        passwd = input('passwd:')
                        if port.isdigit():
                            config.remove_section(host_list[int(host_choise)]) #删掉之前的主机，添加新主机
                            config.write(open(save_path,'w'))
                            config.add_section(hostname)
                            config.set(hostname, 'hostname', hostname)
                            config.set(hostname, 'port', port)
                            config.set(hostname, 'user', user)
                            config.set(hostname, 'passwd', passwd)
                            config.write(open(save_path, 'w'))
                            print('更新成功！')
                        else:
                            print('请输入正确的端口号！')
                    else:
                        print('主机不存在！')
                else:
                    print('输入错误')
            else:
                print('主机组不存在！')
        else:
            print('输入错误！')

    def delete_host(self):
        host_path = setting.HOST_FILE_PATH
        group_list = os.listdir(host_path)
        for index, item in enumerate(group_list):
            print(index, item)
        group_choise = input('请选择主机组>>>')
        if group_choise.isdigit():
            if group_list[int(group_choise)]:
                save_path = os.path.join(setting.HOST_FILE_PATH, group_list[int(group_choise)])
                config = configparser.ConfigParser()
                config.read(save_path)
                host_list = config.sections()
                for index, item in enumerate(host_list):
                    print(index, item)
                host_choise = input('请选择需要删除的主机>>>')
                if host_choise.isdigit():
                    if host_list[int(host_choise)]:
                        config.remove_section(host_list[int(host_choise)]) #删除对应的主机
                        config.write(open(save_path, 'w'))
                        print('删除成功！')
                    else:
                        print('主机不存在！')
                else:
                    print('输入错误！')
            else:
                print('主机组不存在！')
        else:
            print('输入错误！')

    def select_host(self):
        host_path = setting.HOST_FILE_PATH
        group_list = os.listdir(host_path)
        for index, item in enumerate(group_list):
            print(index, item)
        group_choise = input('请选择主机组>>>')
        if group_choise.isdigit():
            if group_list[int(group_choise)]:
                save_path = os.path.join(setting.HOST_FILE_PATH, group_list[int(group_choise)])
                f = open(save_path,'r') #直接遍历对应主机组文件
                for i in f:
                    print(i.strip())
                f.close()


    def go_back(self):
        back_obj = main.Manage()
        back_obj.user_choise()

# if __name__ == '__main__':
#     obj = Host_manage()
#     obj.menu()



